﻿Imports DTO
Imports System.Data.SqlClient


Public Class LoaiMachDieuKhienDAO

    Function layDSLoaiMDK() As List(Of LoaiMachDieuKhienDTO)
        Dim SqlConn As SqlClient.SqlConnection = Nothing

        Try
            Dim ret As New List(Of LoaiMachDieuKhienDTO)
            SqlConn = DataProvider.getConnection()
            Dim sql As String = "select * from LoaiMachDieuKhien"
            Dim command As New SqlClient.SqlCommand(sql, SqlConn)
            SqlConn.Open()
            Dim reader As SqlDataReader = command.ExecuteReader()

            While reader.Read()
                Dim mdk As LoaiMachDieuKhienDTO = New LoaiMachDieuKhienDTO()
                mdk.MaLoai = reader("MaLoai")
                mdk.TenLoai = reader("TenLoai")
                ret.Add(mdk)
            End While
            Return ret
        Catch e As Exception
            Throw New Exception("DAO: execute error - layDSLoaiMDK", e)
        Finally
            SqlConn.Close()
        End Try

        Return Nothing
    End Function

    Function themLoaiMDK(ByVal tenLoai As String) As Integer
        Dim SqlConn As SqlClient.SqlConnection = Nothing

        Try
            Dim ret As Integer
            SqlConn = DataProvider.getConnection()
            Dim sql As String = "insert into LoaiMachDieuKhien(TenLoai) values(N'" & tenLoai & "')"
            Dim command As New SqlClient.SqlCommand(sql, SqlConn)
            SqlConn.Open()

            ret = command.ExecuteNonQuery()

            Return ret
        Catch e As Exception
            Throw New Exception("DAO: execute error - themLoaiMDK", e)
        Finally
            SqlConn.Close()
        End Try

        Return Nothing
    End Function

    Function capNhatLMDK(ByVal maLoai As Integer, ByVal tenLoai As String) As Integer
        Dim SqlConn As SqlClient.SqlConnection = Nothing

        Try
            Dim ret As Integer
            SqlConn = DataProvider.getConnection()
            Dim sql As String = "update LoaiMachDieuKhien set TenLoai = N'" & tenLoai & "' where MaLoai=" & maLoai
            Dim command As New SqlClient.SqlCommand(sql, SqlConn)
            SqlConn.Open()

            ret = command.ExecuteNonQuery()

            Return ret
        Catch e As Exception
            Throw New Exception("DAO: execute error - themLoaiMDK", e)
        Finally
            SqlConn.Close()
        End Try

        Return Nothing
    End Function

    Function xoaLoaiMDK(ByVal maLoai As Integer) As Integer
        Dim SqlConn As SqlClient.SqlConnection = Nothing

        Try
            Dim ret As Integer
            SqlConn = DataProvider.getConnection()
            Dim sql As String = "delete from LoaiMachDieuKhien where MaLoai=" & maLoai
            Dim command As New SqlClient.SqlCommand(sql, SqlConn)
            SqlConn.Open()

            ret = command.ExecuteNonQuery()

            Return ret
        Catch e As Exception
            Throw New Exception("DAO: execute error - themLoaiMDK", e)
        Finally
            SqlConn.Close()
        End Try

        Return Nothing
    End Function

End Class
